package com.dyna.cp.redis.controller;

import com.dyna.cp.redis.service.DynaRedisService;
import com.dyna.domain.resp.R;
import com.dyna.domain.resp.ResultCode;
import com.dyna.utils.RdsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhoucaiwang
 */

@Api(value = "demo模块/cp/redis")
@RestController
@RequestMapping(value = "/redis")
@AllArgsConstructor
public class DynaRedisController {

    public static final List<Object> list = new ArrayList<>();

    private DynaRedisService dynaRedisService;

    @ApiOperation(value = "分布式锁")
    @RequestMapping("/lock")
    public R<Object> lock() {
        dynaRedisService.lock();
        return R.success(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "发布消息")
    @RequestMapping("/publish")
    public R<Boolean> publish(@RequestParam("topic") String topic,
                              @RequestParam("message") String message) {
        RdsUtil.publishMessage(topic, message);
        return R.isSuccess(Boolean.TRUE);
    }

    @ApiOperation("stream消息队列send")
    @RequestMapping("/streamSend")
    public R<Object> streamSend(@RequestParam("msg") String msg) {
        Map<String, String> map = new HashMap<>();
        map.put("msg", msg);
        RecordId recordId = RdsUtil.getOpsForStream().add("test5", map);
        return R.data(recordId);
    }

    @ApiOperation("redis布隆过滤器添加元素")
    @GetMapping("/addBloomFilterData")
    public R<Boolean> addBloomFilterData(@RequestParam("filterName") String filterName,
                                         @RequestParam("element") String element
                                         ) {
        dynaRedisService.addBloomFilterData(filterName, element);
        return R.isSuccess(Boolean.TRUE);
    }

    @ApiOperation("测试是否存在布隆过滤器中")
    @GetMapping("/foundElement")
    public R<Boolean> foundElement(@RequestParam("filterName") String filterName,
                                   @RequestParam("element") String element) {
        return R.isSuccess(dynaRedisService.foundElement(filterName, element));
    }

    @ApiOperation("初始化票数")
    @GetMapping("/initTicket")
    public R<Boolean> initTicket(@RequestParam("count") Integer count) {
        return R.isSuccess(dynaRedisService.initTicket(count));
    }

    @ApiOperation("redis脚本实现抢票")
    @GetMapping("/ticket")
    public R<Long> ticket() {
        return R.data(dynaRedisService.ticket());
    }

    @ApiOperation("延时消息通知")
    @GetMapping("/delay/msg")
    public R<Boolean> sendMessage(@RequestParam Integer type,
                                  @RequestParam String content,
                              @RequestParam long delaySeconds) {
        return R.isSuccess(dynaRedisService.delayMsg(type, content, delaySeconds));
    }

    @ApiOperation("lua脚本")
    @GetMapping("/script")
    public R<Object> script() {
        return R.data(dynaRedisService.script());
    }

}